<?php

/*************************************************************************
Class Pick_Core_Languages
----------------------------------------------------------------
PICKCO CMS Project
Company: PICKCO  Co., Ltd  
Email: admin@pick.vn
Last updated: 15/07/2008
**************************************************************************/
class Pick_Core_Languages extends Pick_Core_Objects {
	
	function Pick_Core_Languages($database = '') {
		if(!$database) {
			global $db;
			$this->_db = $db;
		} else $this->_db = $database;
		$this->objectInfo 	= 'Pick_Core_LanguagesInfo';		
		$this->table = DB_PREFIX."languages";
	}	

/*-----------------------------------------------------------------------*
* Function: getArrayCode()
* Parameter: WHERE 1=1
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getArrayCode() {
		$results = $this->getObjects();		
		$arrLang = array();
		if($results) {				
			foreach($results['data'] as $obj){
				$arrLang[] = $obj->getField("code");	
			}
		}
		return $arrLang;
	}	
/*-----------------------------------------------------------------------*
* Function: getDefaultLanguages()
* Parameter: WHERE 1=1
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getDefaultLanguage() {
		$results = $this->getObjects(1, '`default` = 1', '', 1);
		if($results) {
			foreach($results['data'] as $obj){
				return $obj->getField("code");	
			}
		}
		return; 
	}
/*-----------------------------------------------------------------------*
* Function: addNewFieldIntoTable
* Parameter: WHERE $lang
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function addNewFieldIntoTable($lang) {
		$allTable = $this->showTables();
		$arrLang = $this->getArrayCode();
		$arrLang = array_slice($arrLang, 0, count($arrLang)-1); // Cut new item has just add
		foreach($allTable as $table){
			$allCol = $this->showColumns($table);
			$arrNeedAddLang = array();
			foreach($allCol as $col){
				$nameColumn = $col['Field'];
				$suffix = explode("_", $nameColumn);
				if(in_array($suffix[0], $arrLang)){
					$arrNeedAddLang[$suffix[1]] = $suffix[0];
				}#endif
			}#endforeach
			if(!in_array($lang, $arrLang) && count($arrNeedAddLang)){
				
				foreach($arrNeedAddLang as $nameField => $codeLang){
					$strSQL = "";
					if(in_array($nameField, array("description", "details", "about", "address", "abstract"))){
						$strSQL .= " ALTER TABLE `".$table."` ADD `".$lang."_".$nameField."` TEXT NULL AFTER `".$codeLang."_".$nameField."`";
					}else{
						$strSQL .= " ALTER TABLE `".$table."` ADD `".$lang."_".$nameField."` VARCHAR( 255 ) NULL AFTER `".$codeLang."_".$nameField."`";
					}
					$this->_db->query($strSQL);
				}#endforeach
			}
		}#endforeach
		return true;
	}
/*-----------------------------------------------------------------------*
* Function: updateFieldIntoTable
* Parameter: WHERE $lang
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function updateFieldIntoTable($langNew, $langOld) {
		$allTable = $this->showTables();
		$arrLang = $this->getArrayCode();
		foreach($allTable as $table){
			$allCol = $this->showColumns($table);
			$arrNeedUpdateLang = array();
			foreach($allCol as $col){
				$nameColumn = $col['Field'];
				$suffix = explode("_", $nameColumn);
				if(in_array($suffix[0], $arrLang)){
					$arrNeedUpdateLang[$suffix[1]] = $suffix[0];	
				}#endif
			}#endforeach		
			if(count($arrNeedUpdateLang)){
				
				foreach($arrNeedUpdateLang as $nameField => $codeLang){
					$strSQL = "";
					if(in_array($nameField, array("description", "details", "about", "address", "abstract"))){
						$strSQL .= " ALTER TABLE `".$table."` CHANGE `".$langOld."_".$nameField."` `".$langNew."_".$nameField."` TEXT NULL";
					}else{
						$strSQL .= " ALTER TABLE `".$table."` CHANGE `".$langOld."_".$nameField."` `".$langNew."_".$nameField."` VARCHAR( 255 ) NULL";
					}
					$ok = $this->_db->query($strSQL);
                    $strSQL = "";
                    if(!$ok){
                         
    					if(in_array($nameField, array("description", "details", "about", "address", "abstract"))){
    						$strSQL .= " ALTER TABLE `".$table."` ADD `".$langNew."_".$nameField."` TEXT NULL AFTER `".$codeLang."_".$nameField."`";
    						
    					}else{
    						$strSQL .= " ALTER TABLE `".$table."` ADD `".$langNew."_".$nameField."` VARCHAR( 255 ) NULL AFTER `".$codeLang."_".$nameField."`";
    					}
    					$this->_db->query($strSQL);
                        
                    }					
				}#endforeach
											
			}			
		}#endforeach
		return true;
	}
/*-----------------------------------------------------------------------*
* Function: updateFieldIntoTable
* Parameter: WHERE $lang
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function deleteFieldIntoTable($lang) {
		$allTable = $this->showTables();
		$arrLang = $this->getArrayCode();
		foreach($allTable as $table){
			$allCol = $this->showColumns($table);
			$arrNeedUpdateLang = array();
			foreach($allCol as $col){
				$nameColumn = $col['Field'];
				$suffix = explode("_", $nameColumn);
				if(in_array($suffix[0], $arrLang)){
					$arrNeedUpdateLang[$suffix[1]] = $suffix[0];	
				}#endif
			}#endforeach		
			if(count($arrNeedUpdateLang)){
				
				foreach($arrNeedUpdateLang as $nameField => $codeLang){
					$strSQL = "";
					$strSQL .= " ALTER TABLE `".$table."` DROP `".$lang."_".$nameField."`";
					$this->_db->query($strSQL);
				}#endforeach
			}			
		}#endforeach
		return true;
	}
/*-----------------------------------------------------------------------*
* Function: getArrayMessages
* Parameter: WHERE $lang
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getArrayMessages(){
		global $lang;
		$sql = "
			SELECT ld.default_text, lo.translate_text
			FROM  `n_languages_default` AS ld
			LEFT JOIN  `n_languages_other` AS lo ON lo.`language_default_id` = ld.id
			AND lo.`code` = '$lang'
		";
		$result = $this->_db->query($sql);
		unset($sql);
		if($result) {
			$data = array();
			while($row = $this->_db->fetchArray($result)) {
				$data[$row['default_text']] = $row['translate_text'];
			}
			$this->_db->freeResult($result);
			unset($result);
			return $data;
		}
		return;
	}
}
?>